defmodule Pango.LogConfig do
	@moduledoc false

	require Logger

	def config_log() do
		Logger.configure_backend(
			:console,
			[
				format: "$time [$level] $metadata $levelpad$message\n",
				metadata: [:module, :line]
			]
		)
		roll_error_logger()
		#		Logger.configure(level: :info)
		set_error()
	end

	def set_error() do
		Logger.configure(level: :error)
	end

	def set_info() do
		Logger.configure(level: :info)
	end

	@doc """
		每天一个日志文件
		可以通过 Logger.configure(level: *) 来改变日志的等级
		需要配置 LOG_PATH
	"""
	# prod 模式, error 配置
	def roll_error_logger() do
		Logger.debug("-------- roll_error_logger ----------")
		{{yy, mm, dd}, {_h, _m, _s}} = :calendar.local_time()
		time_str = "#{yy}_#{mm}_#{dd}-0-0"
		base_path = AppConfigure.fetch("LOG_PATH")
		Logger.add_backend({LoggerFileBackend, :error_log})
		path = base_path <> time_str <> ".log"
		Logger.debug("now log to: #{inspect(path)}")
		Logger.configure_backend(
			{LoggerFileBackend, :error_log},
			path: path,
			format: "$time [$level] $metadata\t $levelpad$message\n",
			level: :info,
			metadata: [:module, :line]
		)
		now = NaiveDateTime.local_now()
		next_roll_ts =
			now
			|> Timex.end_of_day()
			|> Timex.shift(seconds: 2)
			|> Timex.diff(now, :milliseconds)

		:timer.apply_after(next_roll_ts, __MODULE__, :roll_error_logger, [])
	end

end
